#include <unos_input.h>
#include <stdint.h>



#define CONFIG_USE_VKCODE_3 (1) // debug
#define CONFIG_GETKEYCODE_USART (1)

#ifdef CONFIG_USE_VKCODE_3
	#include <usart.h>
#endif

#ifdef CONFIG_USE_VKCODE_3
/*
上下左右
3  [1b]  [4f]  [41]
3  [1b]  [4f]  [42]
3  [1b]  [4f]  [44]
3  [1b]  [4f]  [43]

#define VK_LEFT (37)
#define VK_UP (38)
#define VK_RIGHT (39)
#define VK_DOWN (40)

F1~F4
3  [1b]  [4f]  [50]
3  [1b]  [4f]  [51]
3  [1b]  [4f]  [52]
3  [1b]  [4f]  [53]

*/
static int vkcode_3(char *keys)
{
	if (keys[0] == 0x1b && keys[1] == 0x4f) {
		// 上下左右
		if (keys[2] <= 0x44) {
			switch(keys[2]) {
			case 0x41:
				return VK_UP;
			case 0x42:
				return VK_DOWN;
			case 0x44:
				return VK_LEFT;
			case 0x43:
				return VK_RIGHT;
			default:
				break;
			}
		}
#ifdef CONFIG_VKF1_F4
		// F1~F4
		else if (keys[2] <= 0x53) {
			return VK_F1 + keys[2] - 0x50;
		}
#endif
	}
	return 0;
}
#endif // CONFIG_USE_VKCODE_3

#ifdef CONFIG_USE_VKCODE_5
/*
5  [1b]  [5b]  [31]  [35]  [7e]
5  [1b]  [5b]  [31]  [37]  [7e]
5  [1b]  [5b]  [31]  [38]  [7e]
5  [1b]  [5b]  [31]  [39]  [7e]
5  [1b]  [5b]  [32]  [30]  [7e]
5  [1b]  [5b]  [32]  [31]  [7e]
5  [1b]  [5b]  [32]  [33]  [7e]
5  [1b]  [5b]  [32]  [34]  [7e]
*/
static int vkcode_5(char *keys)
{
	// F5~F12
	if (keys[0] + keys[1] + keys[2] + keys[4] == (0x1b +  0x5b + 0x31 + 0x7e) ) {
		switch(keys[3]) {
		case 0x35:
			return VK_F5;
		case 0x37:
			return VK_F6;
		case 0x38:
			return VK_F7;
		case 0x39:
			return VK_F8;
		default:
			break;
		}
	} else if (keys[0] + keys[1] + keys[2] + keys[4] == (0x1b +  0x5b + 0x32 + 0x7e) ) {
		switch(keys[3]) {
		case 0x30:
			return VK_F9;
		case 0x31:
			return VK_F10;
		case 0x33:
			return VK_F11;
		case 0x34:
			return VK_F12;
		default:
			break;
		}
	}
	return 0;
}
#endif // End CONFIG_USE_VKCODE_5


#ifdef CONFIG_USE_VKCODE_4
/*
insert delete Home End pageUp pageDown
4  [1b]  [5b]  [32]  [7e]
4  [1b]  [5b]  [33]  [7e]
4  [1b]  [5b]  [31]  [7e]
4  [1b]  [5b]  [34]  [7e]
4  [1b]  [5b]  [35]  [7e]
4  [1b]  [5b]  [36]  [7e]
*/
static int vkcode_4(char *keys)
{
	// insert delete Home End pageUp pageDown
	if (keys[0] + keys[1] + keys[3] == (0x1b +  0x5b + 0x7e) ) {
		switch(keys[2]) {
#ifdef CONFIG_VK_INSERT
		case 0x32:
			return VK_INSERT;
#endif
#ifdef CONFIG_VK_DELETE
		case 0x33:
			return VK_DELETE;
#endif
#ifdef CONFIG_VK_HOME
		case 0x31:
			return VK_HOME;
#endif
#ifdef CONFIG_VK_END
		case 0x34:
			return VK_END;
#endif
#ifdef CONFIG_VK_PRIOR
		case 0x35:
			return VK_PRIOR;
#endif
#ifdef CONFIG_VK_NEXT
		case 0x36:
			return VK_NEXT;
#endif
		default:
			break;
		}
	}
	return 0;
}
#endif  // CONFIG_USE_VKCODE_4

/**
 * @brief	从串口读取键盘内容
 * @param	null
 * @retval	null
 * @remarks
 * @see
 */

int getkeycode()
{
	int len;
	char keys[5];
	int i;

#ifdef CONFIG_GETKEYCODE_USART
	// len = CommRead(keys, 5);
	len = ReadUart(keys, 5);
#endif

#ifdef CONFIG_DBG_UNOS_INPUT
// #if 1
	for (i = 0; i < len; i++) {
		if (len) {
			printf("%d  ", len);
			for (i = 0; i < len; i++) {
				printf("[%x]  ", keys[i]);
			}
			printf("\r\n");
		}
	}
#endif
	// 需要对读取时间严格把控，否则case 3 4 5 读取长度不一定
	switch (len) {
	case 1:
		return keys[0];
#ifdef CONFIG_USE_VKCODE_3
	case 3:
		return vkcode_3(keys);
#endif
#ifdef CONFIG_USE_VKCODE_4
	case 4:
		return vkcode_4(keys);
#endif
#ifdef CONFIG_USE_VKCODE_5
	case 5:
		return vkcode_5(keys);
#endif
	default:
		break;
	}
	return 0;
}

